package com.suyafei.exception;

import com.suyafei.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: suyafei-1024-shop
 * @description
 * @author: luffy
 * @create: 2021-01-28 15:53
 */
@ControllerAdvice
@Slf4j
public class CustomExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData handle(Exception e) {
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            log.error("[业务异常{}]", e);
            return JsonData.buildCodeAndMsg(bizException.getCode(), bizException.getMsg());
        }else {
            log.error("[非业务异常{}]", e);
            return JsonData.buildError("全局异常，未知错误");
        }
    }
}
